﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using System.Net.Sockets;
using System.Runtime.CompilerServices;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using IPC.Communication.Framework.BasicFramework;

namespace IPC.Communication.Framework
{
    public static class HslExtension
	{
        //[CompilerGenerated]
        //[Serializable]
        //private sealed class <>c__25<T>
        //{
        //    public static readonly HslExtension.<>c__25<T> <>9 = new HslExtension.<>c__25<T>();

        //    public static Func<string, string> <>9__25_0;

        //    internal string <ToStringArray>b__25_0(string m)
        //    {
        //        return m;
        //    }
        //}

		public static string ToHexString(this byte[] InBytes)
		{
			return SoftBasic.ByteToHexString(InBytes);
		}

		public static string ToHexString(this byte[] InBytes, char segment)
		{
			return SoftBasic.ByteToHexString(InBytes, segment);
		}

		public static string ToHexString(this byte[] InBytes, char segment, int newLineCount)
		{
			return SoftBasic.ByteToHexString(InBytes, segment, newLineCount);
		}

		public static byte[] ToHexBytes(this string value)
		{
			return SoftBasic.HexStringToBytes(value);
		}

		public static byte[] ToByteArray(this bool[] array)
		{
			return SoftBasic.BoolArrayToByte(array);
		}

		public static bool[] ToBoolArray(this byte[] InBytes, int length)
		{
			return SoftBasic.ByteToBoolArray(InBytes, length);
		}

		public static T[] ReverseNew<T>(this T[] value)
		{
			T[] array = value.CopyArray<T>();
			Array.Reverse(array);
			return array;
		}

		public static bool[] ToBoolArray(this byte[] InBytes)
		{
			return SoftBasic.ByteToBoolArray(InBytes);
		}

		public static bool GetBoolValue(this byte[] bytes, int bytIndex, int boolIndex)
		{
			return SoftBasic.BoolOnByteIndex(bytes[bytIndex], boolIndex);
		}

		public static bool GetBoolByIndex(this byte[] bytes, int boolIndex)
		{
			return SoftBasic.BoolOnByteIndex(bytes[boolIndex / 8], boolIndex % 8);
		}

		public static bool GetBoolByIndex(this byte byt, int boolIndex)
		{
			return SoftBasic.BoolOnByteIndex(byt, boolIndex % 8);
		}

		public static byte SetBoolByIndex(this byte byt, int boolIndex, bool value)
		{
			return SoftBasic.SetBoolOnByteIndex(byt, boolIndex, value);
		}

		public static T[] RemoveDouble<T>(this T[] value, int leftLength, int rightLength)
		{
			return SoftBasic.ArrayRemoveDouble<T>(value, leftLength, rightLength);
		}

		public static T[] RemoveBegin<T>(this T[] value, int length)
		{
			return SoftBasic.ArrayRemoveBegin<T>(value, length);
		}

		public static T[] RemoveLast<T>(this T[] value, int length)
		{
			return SoftBasic.ArrayRemoveLast<T>(value, length);
		}

		public static T[] SelectMiddle<T>(this T[] value, int index, int length)
		{
			return SoftBasic.ArraySelectMiddle<T>(value, index, length);
		}

		public static T[] SelectBegin<T>(this T[] value, int length)
		{
			return SoftBasic.ArraySelectBegin<T>(value, length);
		}

		public static T[] SelectLast<T>(this T[] value, int length)
		{
			return SoftBasic.ArraySelectLast<T>(value, length);
		}

		public static T GetValueOrDefault<T>(JObject jObject, string name, T defaultValue)
		{
			return SoftBasic.GetValueFromJsonObject<T>(jObject, name, defaultValue);
		}

		public static T[] SpliceArray<T>(this T[] value, params T[][] arrays)
		{
			List<T[]> list = new List<T[]>(arrays.Length + 1);
			list.Add(value);
			list.AddRange(arrays);
			return SoftBasic.SpliceArray<T>(list.ToArray());
		}

		public static T[] IncreaseBy<T>(this T[] array, T value)
		{
			bool flag = typeof(T) == typeof(byte);
			if (flag)
			{
				ParameterExpression parameterExpression = Expression.Parameter(typeof(int), "first");
				ParameterExpression parameterExpression2 = Expression.Parameter(typeof(int), "second");
				Expression body = Expression.Add(parameterExpression, parameterExpression2);
				Expression<Func<int, int, int>> expression = Expression.Lambda<Func<int, int, int>>(body, new ParameterExpression[]
				{
					parameterExpression,
					parameterExpression2
				});
				Func<int, int, int> func = expression.Compile();
				for (int i = 0; i < array.Length; i++)
				{
					array[i] = (T)((object)((byte)func(Convert.ToInt32(array[i]), Convert.ToInt32(value))));
				}
			}
			else
			{
				ParameterExpression parameterExpression3 = Expression.Parameter(typeof(T), "first");
				ParameterExpression parameterExpression4 = Expression.Parameter(typeof(T), "second");
				Expression body2 = Expression.Add(parameterExpression3, parameterExpression4);
				Expression<Func<T, T, T>> expression2 = Expression.Lambda<Func<T, T, T>>(body2, new ParameterExpression[]
				{
					parameterExpression3,
					parameterExpression4
				});
				Func<T, T, T> func2 = expression2.Compile();
				for (int j = 0; j < array.Length; j++)
				{
					array[j] = func2(array[j], value);
				}
			}
			return array;
		}

		public static T[] CopyArray<T>(this T[] value)
		{
			bool flag = value == null;
			T[] result;
			if (flag)
			{
				result = null;
			}
			else
			{
				T[] array = new T[value.Length];
				Array.Copy(value, array, value.Length);
				result = array;
			}
			return result;
		}

		public static string ToArrayString<T>(this T[] value)
		{
			return SoftBasic.ArrayFormat<T>(value);
		}

		public static string ToArrayString<T>(this T[] value, string format)
		{
			return SoftBasic.ArrayFormat<T>(value, format);
		}

		public static T[] ToStringArray<T>(this string value, Func<string, T> selector)
		{
			bool flag = value.IndexOf('[') >= 0;
			if (flag)
			{
				value = value.Replace("[", "");
			}
			bool flag2 = value.IndexOf(']') >= 0;
			if (flag2)
			{
				value = value.Replace("]", "");
			}
			string[] source = value.Split(new char[]
			{
				',',
				';'
			}, StringSplitOptions.RemoveEmptyEntries);
			return source.Select(selector).ToArray<T>();
		}

		public static T[] ToStringArray<T>(this string value)
		{
			Type typeFromHandle = typeof(T);
			bool flag = typeFromHandle == typeof(byte);
			T[] result = null;
            //if (flag)
            //{
            //    result = (T[])value.ToStringArray(new Func<string, byte>(byte.Parse));
            //}
            //else
            //{
            //    bool flag2 = typeFromHandle == typeof(sbyte);
            //    if (flag2)
            //    {
            //        result = (T[])value.ToStringArray(new Func<string, sbyte>(sbyte.Parse));
            //    }
            //    else
            //    {
            //        bool flag3 = typeFromHandle == typeof(bool);
            //        if (flag3)
            //        {
            //            result = (T[])value.ToStringArray(new Func<string, bool>(bool.Parse));
            //        }
            //        else
            //        {
            //            bool flag4 = typeFromHandle == typeof(short);
            //            if (flag4)
            //            {
            //                result = (T[])value.ToStringArray(new Func<string, short>(short.Parse));
            //            }
            //            else
            //            {
            //                bool flag5 = typeFromHandle == typeof(ushort);
            //                if (flag5)
            //                {
            //                    result = (T[])value.ToStringArray(new Func<string, ushort>(ushort.Parse));
            //                }
            //                else
            //                {
            //                    bool flag6 = typeFromHandle == typeof(int);
            //                    if (flag6)
            //                    {
            //                        result = (T[])value.ToStringArray(new Func<string, int>(int.Parse));
            //                    }
            //                    else
            //                    {
            //                        bool flag7 = typeFromHandle == typeof(uint);
            //                        if (flag7)
            //                        {
            //                            result = (T[])value.ToStringArray(new Func<string, uint>(uint.Parse));
            //                        }
            //                        else
            //                        {
            //                            bool flag8 = typeFromHandle == typeof(long);
            //                            if (flag8)
            //                            {
            //                                result = (T[])value.ToStringArray(new Func<string, long>(long.Parse));
            //                            }
            //                            else
            //                            {
            //                                bool flag9 = typeFromHandle == typeof(ulong);
            //                                if (flag9)
            //                                {
            //                                    result = (T[])value.ToStringArray(new Func<string, ulong>(ulong.Parse));
            //                                }
            //                                else
            //                                {
            //                                    bool flag10 = typeFromHandle == typeof(float);
            //                                    if (flag10)
            //                                    {
            //                                        result = (T[])value.ToStringArray(new Func<string, float>(float.Parse));
            //                                    }
            //                                    else
            //                                    {
            //                                        bool flag11 = typeFromHandle == typeof(double);
            //                                        if (flag11)
            //                                        {
            //                                            result = (T[])value.ToStringArray(new Func<string, double>(double.Parse));
            //                                        }
            //                                        else
            //                                        {
            //                                            bool flag12 = typeFromHandle == typeof(DateTime);
            //                                            if (flag12)
            //                                            {
            //                                                result = (T[])value.ToStringArray(new Func<string, DateTime>(DateTime.Parse));
            //                                            }
            //                                            else
            //                                            {
            //                                                bool flag13 = typeFromHandle == typeof(Guid);
            //                                                if (flag13)
            //                                                {
            //                                                    result = (T[])value.ToStringArray(new Func<string, Guid>(Guid.Parse));
            //                                                }
            //                                                else
            //                                                {
            //                                                    bool flag14 = typeFromHandle == typeof(string);
            //                                                    if (!flag14)
            //                                                    {
            //                                                        throw new Exception("use ToArray<T>(Func<string,T>) method instead");
            //                                                    }
            //                                                    Func<string, string> arg_2CF_1;
            //                                                    if ((arg_2CF_1 = HslExtension.<>c__25<T>.<>9__25_0) == null)
            //                                                    {
            //                                                        arg_2CF_1 = (HslExtension.<>c__25<T>.<>9__25_0 = new Func<string, string>(HslExtension.<>c__25<T>.<>9.<ToStringArray>b__25_0));
            //                                                    }
            //                                                    result = (T[])value.ToStringArray(arg_2CF_1);
            //                                                }
            //                                            }
            //                                        }
            //                                    }
            //                                }
            //                            }
            //                        }
            //                    }
            //                }
            //            }
            //        }
            //    }
            //}
			return result;
		}

		public static OperateResult BeginReceiveResult(this Socket socket, AsyncCallback callback, object obj)
		{
			OperateResult result;
			try
			{
				socket.BeginReceive(new byte[0], 0, 0, SocketFlags.None, callback, obj);
				result = OperateResult.CreateSuccessResult();
			}
			catch (Exception ex)
			{
				if (socket != null)
				{
					socket.Close();
				}
				result = new OperateResult(ex.Message);
			}
			return result;
		}

		public static OperateResult BeginReceiveResult(this Socket socket, AsyncCallback callback)
		{
			return socket.BeginReceiveResult(callback, socket);
		}

		public static OperateResult<int> EndReceiveResult(this Socket socket, IAsyncResult ar)
		{
			OperateResult<int> result;
			try
			{
				result = OperateResult.CreateSuccessResult<int>(socket.EndReceive(ar));
			}
			catch (Exception ex)
			{
				if (socket != null)
				{
					socket.Close();
				}
				result = new OperateResult<int>(ex.Message);
			}
			return result;
		}

		public static string[] SplitDot(this string str)
		{
			return str.Split(new char[]
			{
				'.'
			}, StringSplitOptions.RemoveEmptyEntries);
		}

		public static string ToJsonString(this object obj, Formatting formatting = Formatting.Indented)
		{
			return JsonConvert.SerializeObject(obj, formatting);
		}
	}
}
